package com.pug.ksd.filter;

import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.core.Ordered;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

/**
 * Author: lqm
 * create: 2022/5/4 21:11
 * Lover: lqx
 *
 * @see
 */
@Component
public class TokenFilter implements GatewayFilter, Ordered {
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        System.out.println(1);
        String token = exchange.getRequest().getHeaders().getFirst("token");
        if(token==null){
            exchange.getResponse().setStatusCode(HttpStatus.UNAUTHORIZED);
        }
        //拿token和redis中比较，如果不相同就返回。
        return chain.filter(exchange);
    }

    @Override
    public int getOrder() {
        return 1;
    }
}
